//
//  MSContactsCorrectViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/15.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSContactsCorrectViewController.h"
#import "MSContactsCorrectTableVC.h"

@interface MSContactsCorrectViewController ()
@property (nonatomic, strong) MSFilterMultipleViewController   *selectVC;    //选择原因
@property (nonatomic, strong) SXTextView            *remarkTextView;
@property (nonatomic, strong) UILabel               *reasonLabel;
@property (nonatomic, strong) GXButton              *doneBtn;
@property (nonatomic, strong) UIView                *headView;
@property (nonatomic, copy)   NSString              *typeStr;
@property (nonatomic, strong) MSContactsCorrectTableVC *tableVC;
@property (nonatomic, strong) NSMutableArray        *parasArray;
@end

@implementation MSContactsCorrectViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    titleLable.text = @"纠错";
    leftBarBtn.hidden = NO;
}

- (void)initUI{
    self.tableVC = [[MSContactsCorrectTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.contentView.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    self.selectVC = [[MSFilterMultipleViewController alloc] init];
    self.selectVC.delegate = (id<MSFilterMultipleDelegate>)self;
    self.selectVC.showConfirmBtn = YES;
    self.selectVC.isMultipleSelect = YES;
//    self.selectVC.dataArray = [@[@"联系人姓名有误",@"联系人职位有误",@"联系人电话有误",@"联系人公司有误",@"联系人地址有误",@"其他"] mutableCopy];
//    self.selectVC.stateArray = [@[@"NO",@"NO",@"NO",@"NO",@"NO",@"NO"] mutableCopy];
    [self layoutBottomView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    [self.view addGestureRecognizer:tapGesture];
    
    _typeStr = @"";
}

- (void)layoutBottomView{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    footerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableFooterView = footerView;
    
    _headView =[[UIView alloc] initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 50)];
    _headView.backgroundColor = [UIColor whiteColor];
    [footerView addSubview:_headView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectClick)];
    [_headView addGestureRecognizer:tapGesture];
    
    UILabel *leftlabel= [self creatLabel:CGRectMake(12, 15, 60, 20)];
    leftlabel.textColor = kLightBlackColor;
    leftlabel.text = @"选择类型";
    
    self.reasonLabel =  [self creatLabel:CGRectMake(leftlabel.right+10, 15, SCREEN_WIDTH-105, 20)];
    self.reasonLabel.textAlignment = NSTextAlignmentRight;
    self.reasonLabel.text = @"未选择";
    
    UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-20, 17, 8.5, 16.5)];
    arrow.image = [UIImage imageNamed:@"arrow2"];
    [_headView addSubview:arrow];
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, _headView.bottom, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [footerView addSubview:horizonLineView];
    
    self.remarkTextView = [[SXTextView alloc] initWithFrame:CGRectMake(0, horizonLineView.bottom+10, SCREEN_WIDTH, 90)];
    self.remarkTextView.font = HeitiFont(14);
    self.remarkTextView.placeholder = @" 请输入问题描述（100字以内）";
    self.remarkTextView.delegate = (id)self;
    self.remarkTextView.backgroundColor = [UIColor whiteColor];
    [footerView addSubview:self.remarkTextView];

    UIView *horizonLineView2 =[[UIView alloc] initWithFrame:CGRectMake(0, self.remarkTextView.bottom, SCREEN_WIDTH, 0.6)];
    horizonLineView2.backgroundColor = kNormalGray;
    [footerView addSubview:horizonLineView2];
    
    self.doneBtn = [[GXButton alloc] initWithFrame:CGRectMake(12, self.remarkTextView.bottom + 25, SCREEN_WIDTH-24, 45)];
    self.doneBtn.titleLabel.font = HeitiFont(16);
    [self.doneBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.doneBtn.backgroundColor = kBlueColor;
    self.doneBtn.layer.cornerRadius = 2;
    [self.doneBtn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:self.doneBtn];
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(15);
    label.textColor = kDarkGrayColor;
    [_headView addSubview:label];
    return label;
}

- (void)testDataFunc{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    //纠错类型
    NSString *contactsCorrectFile = [documentsPath stringByAppendingPathComponent:@"contactsCorrectFile.plist"];
    NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *codeArray = [[NSMutableArray alloc] initWithCapacity:0];
    if ([fileManager fileExistsAtPath:contactsCorrectFile]) {
        netEngine.contactsCorrectArray = [[NSMutableArray alloc] initWithContentsOfFile:contactsCorrectFile];
        for (NSDictionary *dict in netEngine.contactsCorrectArray) {
            if ([dict isKindOfClass:[NSDictionary class]]) {
                [titleArray addObject:[HDUtil check:dict[@"name"]]];
                [codeArray addObject:@"NO"];
            }
        }
    }
    if (netEngine.contactsCorrectArray.count<1) {
        [netEngine requestContactsCorrectTypeWithTarget:self.VctrID hudView:self.contentView];
    }
    self.selectVC.dataArray = titleArray;
    self.selectVC.stateArray = codeArray;
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"ContactsCorrect"]!=nil){
            [HDUtil showMessage:@"提交成功"];
            [rootNavigation popViewControllerAnimated:YES];
        }
        if (responseDic[@"ContactsCorrectType"]!=nil){
            NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];
            NSMutableArray *codeArray = [[NSMutableArray alloc] initWithCapacity:0];
            NSArray *typeArray = responseDic[@"ContactsCorrectType"];
            for (NSDictionary *dict in typeArray) {
                if ([dict isKindOfClass:[NSDictionary class]]) {
                    [titleArray addObject:[HDUtil check:dict[@"name"]]];
                    [codeArray addObject:@"NO"];
                }
            }
            self.selectVC.dataArray = titleArray;
            self.selectVC.stateArray = codeArray;
        }
    }
}

- (void)doneClick{
    if ([self.reasonLabel.text isEqualToString:@"未选择"]) {
        [HDUtil showMessage:@"请选择纠错类型"];
        return;
    }
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:0];
    for (NSString *codeStr in _parasArray) {
        NSDictionary *dict = @{@"contactsId":[HDUtil check:_peopleId],
                               @"remark":self.remarkTextView.text,
                               @"type":codeStr,
                               };
        [tempArray addObject:dict];
    }
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:tempArray options:NSJSONWritingPrettyPrinted error:nil];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    [netEngine requestContactsCorrectWithTarget:self.VctrID hudView:self.contentView params:dic];
}

- (void)selectClick{
    [self.view endEditing:YES];
    if ([self.selectVC.view superview]) {
        [self.selectVC dismiss];
    } else {
        [self.selectVC showInView:self.contentView];
    }
}

#pragma mark - MSFilterMultipleDelegate
- (void)selectedFilter{
    NSMutableArray *relutArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *relutCodeArray = [[NSMutableArray alloc] initWithCapacity:0];

    for (int i = 0; i< netEngine.contactsCorrectArray.count; i++) {
        NSString *stateStr = self.selectVC.stateArray[i];
        if ([stateStr isEqualToString:@"YES"]) {
            [relutArray addObject:self.selectVC.dataArray[i]];
            NSDictionary *dict = netEngine.contactsCorrectArray[i];
            [relutCodeArray addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    NSString *resultStr = [relutArray componentsJoinedByString:@"、"];
    self.reasonLabel.text = resultStr.length ? resultStr:@"未选择";
    
    _parasArray = relutCodeArray;
    _typeStr = [relutCodeArray componentsJoinedByString:@","];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSInteger newLength = textView.text.length+text.length-range.length;
    if (!text.length||newLength<=100) {
        return YES;
    } else {
        return NO;
    }
}

- (void)tapClick{
    [self.view endEditing:YES];
}

@end
